var gulp = require('gulp');
var less = require('gulp-less'); //sass工具
var minifycss = require('gulp-minify-css'); //压缩样式工具
var concat = require('gulp-concat'); //合并工具
var connect = require('gulp-connect');
var rename = require('gulp-rename'); //重命名工具
var uglify = require('gulp-uglify'); //压缩工具
var imagemin = require('gulp-imagemin'); //压缩图片工具
var fileinclude = require('gulp-file-include'); //html模板工具
var browserSync = require('browser-sync').create(); //链接工具

gulp.task('serve', ['less', 'js', 'imagemin', 'html', 'serviceJs'], function() {
    browserSync.init({
        server: "./dist"
    });
    gulp.watch('less/**/*.less', ['less']);
    gulp.watch('js/component/*.js', ['js']);
    gulp.watch('js/*.js', ['serviceJs']);
    gulp.watch('html/*.html', ['html']);
    gulp.watch('images/*.{jpg,png}', ['imagemin']);
    gulp.watch("dist/*.html").on('change', browserSync.reload);
});
gulp.task('html', function() {
    return gulp.src('html/*.html')
        .pipe(fileinclude())
        .pipe(gulp.dest('dist/'));
});

gulp.task('less', function() {
    return gulp.src('less/*.less')
        .pipe(less())
        .pipe(minifycss())
        .pipe(gulp.dest('dist/css'));
});

gulp.task('js', function() {
    return gulp.src([
            'js/component/*.js'
        ])
        .pipe(concat('script.js'))
        .pipe(gulp.dest('dist/js'))
        .pipe(uglify())
        .pipe(rename('script.min.js'))
        .pipe(gulp.dest('dist/js'));


});

gulp.task('serviceJs', function() {
    gulp.src('js/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'));
});

gulp.task('imagemin', function() {
    return gulp.src('images/*.{jpg,png,JPG}')
        .pipe(imagemin())
        .pipe(gulp.dest('dist/images'));
});

gulp.task('default', ['serve']);